package com.example.controller;

import com.example.ReturnFormat;
import com.example.pojo.Picture;
import com.example.services.PictureServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.PaginationHelper;

/**
 * @Author 樊政启
 * @Date 2017/11/27
 */
@Controller
@RequestMapping("/picture")
public class PictureContrller {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private PictureServiceImpl pictureService;

    @ResponseBody
    @RequestMapping("/putPicture")
    public String putPicture (@RequestParam("userCode") String userCode,
                              @RequestParam("pictureUrl") String pictureUrl,
                              @RequestParam("pictureDescription") String pictureDescription,
                              @RequestParam("time") int time) {
        logger.debug("putPicture:" + userCode);
        if (pictureService.putPicture(userCode, pictureUrl, pictureDescription, time)) {
            return ReturnFormat.retParam(200, null);
        }
        return ReturnFormat.retParam(400, null);
    }

    @ResponseBody
    @RequestMapping("/getPictureList")
    public PaginationHelper pictureList (@RequestParam("userCode") String userCode,
                                         @RequestParam("page") int page,
                                         @RequestParam("pageSize") int pageSize) {
        PaginationHelper p = pictureService.getPictures(userCode, page, pageSize);
        logger.info(p.getRows().toString());
        return p;
    }
}
